import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Edge()
driver.get('https://www.jiuxian.com/')
driver.maximize_window()
time.sleep(2)
# 点击请登录
driver.find_element(By.CLASS_NAME,'headerLogin').click()
# driver.find_element(By.XPATH,'//a[@href="https://login.jiuxian.com/login.htm"]').click()
# 隐式等待
driver.implicitly_wait(10)
# 输入用户名密码
driver.find_element(By.ID,'userName').send_keys('15235688537')
driver.find_element(By.ID,'password').send_keys('Aa123123')
# 强制等待
time.sleep(15)
# 显式等待
dl = WebDriverWait(driver,20,0.5).until(EC.presence_of_element_located((By.ID,'save')))
dl.click()
time.sleep(3)
# 搜索框
search = driver.find_element(By.ID,'wd')
search.send_keys("五粮液")
time.sleep(2)
ActionChains(driver).double_click(search).click(search).perform()
search.send_keys(Keys.ENTER)
driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
driver.switch_to.window(driver.window_handles[-1])
# driver.switch_to.frame(driver.find_element(By.XPATH,'/html/body/div[5]/div[1]/span'))
time.sleep(3)
dy1 = driver.find_element(By.XPATH,'/html/body/div[5]/div[1]/span').text
print(dy1)
driver.find_element(By.ID,'addToCartForDetail').click()
time.sleep(5)

# driver.find_element(By.CLASS_NAME, 'headerLogin').click()
# # driver.find_element(By.XPATH,'//a[@href="https://login.jiuxian.com/login.htm"]').click()
# # 隐式等待
# driver.implicitly_wait(10)
# # 输入用户名密码
# driver.find_element(By.ID, 'userName').send_keys('15235688537')
# driver.find_element(By.ID, 'password').send_keys('Aa123123')
# # 强制等待
# time.sleep(15)
# # 显式等待
# dl = WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located((By.ID, 'save')))
# dl.click()
# time.sleep(3)
# search = driver.find_element(By.ID, 'wd')
# search.send_keys("五粮液")
# time.sleep(2)
# ActionChains(driver).double_click(search).click(search).perform()
# search.send_keys(Keys.ENTER)
# driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
# driver.switch_to.window(driver.window_handles[-1])
# dy1 = driver.find_element(By.XPATH, '/html/body/div[5]/div[1]/span').text
# print(dy1)
# driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[3]/div[2]/div[1]/a/span').click()
# time.sleep(5)
